package com.rabbit.eap.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rabbit.eap.common.lang.StringUtils;
import com.rabbit.eap.framework.common.api.ApiResult;
import com.rabbit.eap.framework.common.enums.MsgEnum;
import com.rabbit.eap.framework.common.exception.EapException;
import com.rabbit.eap.framework.common.service.impl.BaseServiceImpl;
import com.rabbit.eap.framework.core.pagination.PageInfo;
import com.rabbit.eap.framework.core.pagination.Paging;
import com.rabbit.eap.system.dto.SysDictTypeDTO;
import com.rabbit.eap.system.entity.SysDictData;
import com.rabbit.eap.system.entity.SysDictType;
import com.rabbit.eap.system.enums.DictEnum;
import com.rabbit.eap.system.mapper.SysDictDataMapper;
import com.rabbit.eap.system.mapper.SysDictTypeMapper;
import com.rabbit.eap.system.param.SysDictTypePageParam;
import com.rabbit.eap.system.service.ISysDictTypeService;
import com.rabbit.eap.system.vo.SysDictTypeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @projectName: eap
 * @packageName: com.rabbit.eap.system.service.impl
 * @className SysDictTypeServiceImpl
 * @author： rabbit
 * @version： 1.0
 * @since： 2020/06/06 23:34:02
 * <p>Description:
 * 字典类型表 服务实现类
 * </p>

 */
@Slf4j
@Service
public class SysDictTypeServiceImpl extends BaseServiceImpl<SysDictTypeMapper, SysDictType> implements ISysDictTypeService {

    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     * @param sysDictTypeDTO:sysDictTypeDTO数据传输对象
     * @throws Exception
     * @Description: 添加字典类型表
     * @methodName: saveSysDictType
     * @return: ApiResult
     * @date: 2020/06/06 23:34:02
     * @author: rabbit
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ApiResult saveSysDictType(SysDictTypeDTO sysDictTypeDTO) throws Exception {
        boolean flag = this.checkDictTypeUnique(sysDictTypeDTO);
        if (flag) {
            return ApiResult.fail(sysDictTypeDTO.getDictName() +MsgEnum.IS_EXIST.getDesc());
        }
        SysDictType sysDictType = sysDictTypeDTO.convert(SysDictType.class);
        boolean bool = super.save(sysDictType);
        if (bool) {
            return ApiResult.ok(MsgEnum.ADD_SUCCESS.getDesc());
        } else {
            return ApiResult.fail(MsgEnum.ADD_FAIL.getDesc());
        }
    }

    /**
     * @param sysDictTypeDTO:sysDictTypeDTO数据传输对象
     * @throws Exception
     * @Description 修改字典类型表
     * @methodName: modifySysDictType
     * @return: ApiResult
     * @date: 2020/06/06 23:34:02
     * @author: rabbit
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ApiResult modifySysDictType(SysDictTypeDTO sysDictTypeDTO) throws Exception {
        boolean flag = this.checkDictTypeUnique(sysDictTypeDTO);
        if (flag) {
            return ApiResult.fail(sysDictTypeDTO.getDictName() +MsgEnum.IS_EXIST.getDesc());
        }
        SysDictType sysDictType = sysDictTypeDTO.convert(SysDictType.class);
        boolean bool = super.updateById(sysDictType);
        if (bool) {
            return ApiResult.ok(MsgEnum.UPDATE_SUCCESS.getDesc());
        } else {
            return ApiResult.fail(MsgEnum.UPDATE_FAIL.getDesc());
        }
    }

    /**
     * @param ids:ids集合
     * @throws Exception
     * @Description 删除字典类型表
     * @methodName: removeSysDictType
     * @return: ApiResult
     * @date: 2020/06/06 23:34:02
     * @author: rabbit
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ApiResult removeSysDictType(Long[] ids) throws Exception {
        for(Long dictTypeId :  ids){
           int count =  sysDictDataMapper.selectCount(new LambdaQueryWrapper<SysDictData>().eq(SysDictData::getDictTypeId,dictTypeId));
           if(count>0){
               throw new  EapException(DictEnum.DATA_HAS_EXIST.getCode(),DictEnum.DATA_HAS_EXIST.getDesc());
           }
        }
        boolean bool = super.removeByIds(Arrays.asList(ids));
        if (bool) {
            return ApiResult.ok(MsgEnum.DELETE_SUCCESS.getDesc());
        } else {
            return ApiResult.fail(MsgEnum.DELETE_FAIL.getDesc());
        }
    }

    /**
     * @param id: id
     * @throws Exception
     * @Description 根据Id获取字典类型表详情
     * @methodName: getSysDictTypeInfo
     * @return: ApiResult
     * @date: 2020/06/06 23:34:02
     * @author: rabbit
     */
    @Override
    public ApiResult getSysDictTypeById(Long id) throws Exception {
        if (ObjectUtil.isNull(id)) {
            return ApiResult.fail(MsgEnum.PARAM_NOT_NULL.getDesc());
        }
        SysDictType sysDictType = super.getById(id);
        SysDictTypeVO sysDictTypeVO = sysDictType.convert(SysDictTypeVO.class);
        return ApiResult.ok(sysDictTypeVO);
    }

    /**
     * @param sysDictTypePageParam
     * @throws Exception
     * @Description 字典类型表分页列表
     * @methodName: getSysDictTypePageList
     * @return: ApiResult
     * @date: 2020/06/06 23:34:02
     * @author: rabbit
     */
    @Override
    public ApiResult getSysDictTypePageList(SysDictTypePageParam sysDictTypePageParam) throws Exception {
        Page<SysDictType> page = new PageInfo<>(sysDictTypePageParam, OrderItem.desc("id"));
        QueryWrapper<SysDictType> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(sysDictTypePageParam.getDictName()), SysDictType::getDictName, sysDictTypePageParam.getDictName())
                .like(StringUtils.isNotBlank(sysDictTypePageParam.getDictType()), SysDictType::getDictType, sysDictTypePageParam.getDictType())
                .eq(StringUtils.isNotBlank(sysDictTypePageParam.getStatus()), SysDictType::getStatus, sysDictTypePageParam.getStatus());

        IPage<SysDictTypeVO> iPage = sysDictTypeMapper.selectPage(page, queryWrapper)
                .convert(sysDictType -> sysDictType.convert(SysDictTypeVO.class));
        Paging<SysDictTypeVO> paging = new Paging<SysDictTypeVO>(iPage);
        return ApiResult.ok(paging);
    }

    /**
     * @param dict: 字典类型
     * @Description: 校验字典类型称是否唯一
     * @methodName: checkDictTypeUnique
     * @return: boolean
     * @date: 2020/6/13 20:26
     * @author: rabbit
     */
    public boolean checkDictTypeUnique(SysDictTypeDTO dict) {
        Long dictId = ObjectUtil.isNull(dict.getId()) ? -1L : dict.getId();
        SysDictType dictType = sysDictTypeMapper.selectOne(
                new QueryWrapper<SysDictType>().lambda().eq(SysDictType::getDictType, dict.getDictType()));
        if (ObjectUtil.isNotNull(dictType) && dictType.getId().longValue() != dictId.longValue()) {
            return true;
        }
        return false;
    }

    /**
     * @Description: 获取所有的字典类型
     * @methodName: getAllDictType
     * @param :
     * @return: com.rabbit.eap.framework.common.api.ApiResult
     * @date: 2020/6/13 20:48
     * @author: rabbit
     *
     */
    @Override
    public ApiResult getAllDictType() {
        List<SysDictType> list=  sysDictTypeMapper.selectList(new QueryWrapper<>());
        return ApiResult.ok(list);
    }
    /**
     * @Description: 根据字典类型查询信息
     * @methodName: getDictTypeByType
     * @param dictType: 字典类型
     * @return: com.rabbit.eap.system.entity.SysDictType
     * @date: 2020/6/13 20:52
     * @author: rabbit
     *
     */
    @Override
    public SysDictType getDictTypeByType(String dictType) {
        SysDictType sysDictType = sysDictTypeMapper.selectOne(new QueryWrapper<SysDictType>().lambda().eq(SysDictType::getDictType,dictType));
        return sysDictType;
    }
}
